package com.cskaoyan.mall.search.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.mall.search.api.ProductApi;
import com.cskaoyan.mall.search.converter.ProductConverter;
import com.cskaoyan.mall.search.dto.SearchRequest;
import com.cskaoyan.mall.search.dto.SearchResponse;
import com.cskaoyan.mall.search.entity.ItemDocument;
import com.cskaoyan.mall.search.form.SearchPageInfo;
import com.cskaoyan.mall.search.service.InitDataService;
import com.cskaoyan.mall.search.service.ProductSearchService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: SearchController
 * @Description:
 * @Author: 瞳
 * @Date: 2022/6/26 19:57
 */

@RestController
@RequestMapping("/shopping")
public class SearchController {

    @Autowired
    ProductSearchService productSearchService;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ProductApi productApi;

    @Autowired
    InitDataService initDataService;

    @GetMapping("/search/init")
    public ResponseData init() throws IOException {
        // 找到所有的商品信息
        AllItemResponse allProductItem = productApi.getAllProductItem();

        // 进行类型转化
        List<ItemDocument> itemDocuments = productConverter.items2Documents(allProductItem.getAllItems());

        // 进行es的初始化
        initDataService.initItems();

        return new ResponseUtil().setData(null);
    }

    @GetMapping("/search/{key}")
    public ResponseData searchKey(@PathVariable String key) {

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setKeyword(key);

        // 进行搜索
        SearchResponse searchResponse = productSearchService.fuzzySearch(searchRequest);

        // 进行对象的转化
        List<ItemDocument> itemDocuments = searchResponse.getData();
        List<ProductDto> productDtos = productConverter.items2Dto(itemDocuments);

        return new ResponseUtil().setData(productDtos);
    }

    @PostMapping("/search")
    public ResponseData search(@RequestBody SearchPageInfo searchPageInfo) {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setKeyword(searchPageInfo.getKey());
        searchRequest.setCurrentPage(searchPageInfo.getPage());
        searchRequest.setPageSize(searchPageInfo.getSize());
        searchRequest.setSort(searchPageInfo.getSort());
        searchRequest.setPriceGt(searchPageInfo.getPriceGt());
        searchRequest.setPriceLte(searchPageInfo.getPriceLte());

        // 进行搜索
        SearchResponse searchResponse = productSearchService.search(searchRequest);

        // 进行对象的转化
        List<ItemDocument> itemDocuments = searchResponse.getData();
        List<ProductDto> productDtos = productConverter.items2Dto(itemDocuments);
        searchResponse.setData(productDtos);

        return new ResponseUtil().setData(searchResponse);
    }
}